﻿using OfficeOpenXml;
using System.IO;
using System;

namespace HDSmartCommunity.BUS
{
    public class KoneVoltageBUS
    {
        public bool InsertExcelData(int nb_voltage, int cd_voltage, int nb_falut, int cd_fault, string devicename, string starttime, string endtime)
        {
            try
            {
                string dirpath = AppDomain.CurrentDomain.BaseDirectory + @"TestData\Charge";
                string filepath = dirpath + @"\" + DateTime.Now.ToString("yyyy-MM-dd") + ".xlsx";
                ExcelPackage.LicenseContext = LicenseContext.NonCommercial;
                FileInfo file = new FileInfo(filepath);
                if (!File.Exists(filepath))
                {
                    using (ExcelPackage package = new ExcelPackage(file))
                    {
                        ExcelWorksheet worksheet = package.Workbook.Worksheets.Add("测试数据");
                        worksheet.Cells[1, 1].Value = "Id";
                        worksheet.Cells[1, 2].Value = "ItemType";
                        worksheet.Cells[1, 3].Value = "ChargeCount";
                        worksheet.Cells[1, 4].Value = "ChargeStartTime";
                        worksheet.Cells[1, 5].Value = "ChargeStopTime";
                        worksheet.Cells[1, 6].Value = "BatteryVoltage";
                        worksheet.Cells[1, 7].Value = "DischargeVoltage";
                        worksheet.Cells[1, 8].Value = "FaultCode";
                        worksheet.Cells[1, 9].Value = "CreateDateTime";
                        worksheet.Cells[2, 1].Value = "Id";
                        worksheet.Cells[2, 2].Value = devicename;
                        worksheet.Cells[2, 3].Value = "1";
                        worksheet.Cells[2, 4].Value = starttime;
                        worksheet.Cells[2, 5].Value = endtime;
                        worksheet.Cells[2, 6].Value = cd_voltage * 0.1;
                        worksheet.Cells[2, 7].Value = nb_voltage * 0.001;
                        worksheet.Cells[2, 8].Value = cd_fault;
                        worksheet.Cells[2, 9].Value = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");

                        package.Save();
                    }
                }
                else
                {
                    using (ExcelPackage package = new ExcelPackage(file))
                    {
                        ExcelWorksheet worksheet = package.Workbook.Worksheets["测试数据"];
                        int rowIndex = worksheet.Dimension.End.Row + 1;
                        worksheet.Cells[rowIndex, 0].Value = "Id";
                        worksheet.Cells[rowIndex, 1].Value = devicename;
                        worksheet.Cells[rowIndex, 2].Value = "1";
                        worksheet.Cells[rowIndex, 3].Value = starttime;
                        worksheet.Cells[rowIndex, 4].Value = endtime;
                        worksheet.Cells[rowIndex, 5].Value = cd_voltage * 0.1;
                        worksheet.Cells[rowIndex, 6].Value = nb_voltage * 0.001;
                        worksheet.Cells[rowIndex, 7].Value = cd_fault;
                        worksheet.Cells[rowIndex, 8].Value = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                        // 保存Excel文件
                        package.Save();
                    }
                    //var workbook = new Workbook(filepath);
                    //var worksheet = workbook.Worksheets[0]; // 获取第一个工作表

                    //// 在工作表的末尾追加一行数据
                    //int rowIndex = worksheet.Cells.MaxDataRow + 1; // 获取当前最大行号加1
                    //worksheet.Cells[rowIndex, 0].Value = "Id";
                    //worksheet.Cells[rowIndex, 1].Value = devicename;
                    //worksheet.Cells[rowIndex, 2].Value = "1";
                    //worksheet.Cells[rowIndex, 3].Value = starttime;
                    //worksheet.Cells[rowIndex, 4].Value = endtime;
                    //worksheet.Cells[rowIndex, 5].Value = cd_voltage * 0.1;
                    //worksheet.Cells[rowIndex, 6].Value = nb_voltage * 0.001;
                    //worksheet.Cells[rowIndex, 7].Value = cd_fault;
                    //worksheet.Cells[rowIndex, 8].Value = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                    //// 保存Excel文件
                    //workbook.Save(filepath);
                }
            }
            catch
            {
            }
            return true;
        }
    }
}
